﻿using UnityEngine;
using System.Collections;
using UnityEngine.UI;

namespace UEngine
{
    public class DebugMessageBoxCom : MonoBehaviour
    {

        void Start()
        {
            initButtonOK();
        }

        public void ShowMessage(string message)
        {
            GameObject textGo = FindContorl("Image/Text");
            if (textGo == null)
            {
                return;
            }

            Text text = textGo.GetComponent<Text>();
            if (text == null)
            {
                return;
            }

            text.text +="\n------------------\n" + message;

            transform.SetAsLastSibling();
            gameObject.SetActive(true);

        }

        void initButtonOK()
        {
            GameObject buttonGo = FindContorl("Button");
            if (buttonGo == null)
            {
                Debug.Log("buttonGo == null");
                return;
            }
            Debug.Log(buttonGo.name);
            Button button = buttonGo.GetComponent<Button>();
            button.onClick.AddListener(OnClickButtonOK);
        }

        GameObject FindContorl(string name)
        {
            Transform transform = gameObject.transform.Find(name);
            if (transform == null)
            {
                return null;
            }
            Debug.Log(transform.name);
            Debug.Log(transform.gameObject.name);
            return transform.gameObject;
        }

        void OnClickButtonOK()
        {
            Debug.Log("!!!!!");
            gameObject.SetActive(false);
        }

    }
    
}
